#ifndef __ZHONG_USER__
#define __ZHONG_USER__
#include "Factory.hpp"
#include "user.hxx"
#include "user-odb.hxx"

// #include "../odb/user.hxx"
// #include "../user_server/test/mysql_test/user-odb.hxx"
namespace bite_im
{

    using UserPtr = std::shared_ptr<User>;
    class UserTable
    {
    public:
        using ptr = std::shared_ptr<UserTable>;
        UserTable() {}
        UserTable(const DBPtr &db) : _db(db) {}

        bool insert(const UserPtr &user)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());

                _db->persist(*user);
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("新增用户[{}]出现异常，原因[{}]", user->nickname(), e.what());
                return false;
            }
            return true;
        }
        bool update(const UserPtr &user)
        {
            try
            {
                // 获取事务对象，开启事务
                odb::transaction transaction(_db->begin());

                _db->update(*user);
                // 提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("更新用户[{}]出现异常，原因[{}]", user->nickname(), e.what());
                return false;
            }
            return true;
        }
        UserPtr select_by_id(const std::string &user_id)
        {
            UserPtr user;
            try
            {
                // 3.获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<User>;
                // using result = odb::result<User>;
                // query::user_id == user_id 会组织为一个SQL语句
                user.reset(_db->query_one<User>(query::user_id == user_id));

                // 4.提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("通过用户id[{}]查询出现异常，原因[{}]", user_id, e.what());
            }
            return user;
        }
        UserPtr select_by_phone(const std::string &phone)
        {
            UserPtr user;
            try
            {
                // 3.获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<User>;
                // using result = odb::result<User>;
                // query::user_id == user_id 会组织为一个SQL语句
                user.reset(_db->query_one<User>(query::phone == phone));

                // 4.提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("通过用户手机号[{}]查询出现异常，原因[{}]", phone, e.what());
            }
            return user;
        }
        UserPtr select_by_nickname(const std::string &nickname)
        {
            UserPtr user;
            try
            {
                // 3.获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<User>;
                // using result = odb::result<User>;
                // query::user_id == user_id 会组织为一个SQL语句
                user.reset(_db->query_one<User>(query::nickname == nickname));

                // 4.提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("通过用户昵称[{}]查询出现异常，原因[{}]", nickname, e.what());
            }
            return user;
        }
        std::vector<UserPtr> select_multi_users(const std::vector<std::string> &id_list)
        {
            std::vector<UserPtr> user_list;
            try
            {
                // 3.获取事务对象，开启事务
                odb::transaction transaction(_db->begin());
                using query = odb::query<User>;
                using result = odb::result<User>;
                // 组织sql语句--查询条件
                std::stringstream sql_ss;
                sql_ss << "user_id in (";
                for (auto &id : id_list)
                {
                    sql_ss << "'" << id << "',";
                }
                std::string sql = sql_ss.str();
                sql.pop_back(); // 删掉最后一个尾字符
                sql = sql + ")";
                // 将获取的对象传入到ret中
                result ret(_db->query<User>(sql));
                for (auto it = ret.begin(); it != ret.end(); ++it)
                {
                    auto user = std::make_shared<User>(*it);
                    user_list.push_back(user);
                }
                // 4.提交事务
                transaction.commit();
            }
            catch (std::exception &e)
            {
                LOG_ERROR("通过用户id列表查询出现异常，原因[{}]", e.what());
            }
            return user_list;
        }

    private:
        DBPtr _db;
    };
}

#endif